package life.cqq.jwt.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.Map;

@Component
public class SpringContextUtils implements ApplicationContextAware {

    /**
     * 开发环境
     */
    public static final String DEVELOPMENT_ENVIRONMENT = "dev";

    /**
     * 测试环境
     */
    public static final String TEST_ENVIRONMENT = "test";

    /**
     * 线上环境
     */
    public static final String PRODUCT_ENVIRONMENT = "prod";

    /**
     * Spring上下文环境实例
     */
    public static ApplicationContext applicationContext;

    /**
     * 注入Spring上下文
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    /**
     * ==================== Basic function ====================
     */

    /**
     * 根据BeanName获取Bean
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    /**
     * 根据BeanName获取Bean
     * @param requiredType
     * @return
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * 根据BeanName与Type获取Bean
     * @param name
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    /**
     * 判断是否包含Bean
     * @param name
     * @return
     */
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    /**
     * 判断Bean是否为单例
     * @param name
     * @return
     */
    public static boolean isSingleton(String name) {
        return applicationContext.isSingleton(name);
    }

    /**
     * 判断Bean是否为多例
     * @param name
     * @return
     */
    public static boolean isPrototype(String name) {
        return applicationContext.isPrototype(name);
    }

    /**
     * 获取上下文资源
     * @param location
     * @return
     */
    public static Resource getResource(String location) {
        return applicationContext.getResource(location);
    }

    /**
     * 获取标记了指定注解的Bean列表
     * @param annotationClass
     * @return
     */
    public static Map<String, Object> getBeansWithAnnotation(
        Class<? extends Annotation> annotationClass) {
        return applicationContext.getBeansWithAnnotation(annotationClass);
    }

    /**
     * 获取指定类型的Bean列表
     * @param typeClass
     * @return
     */
    public static <T> Map<String, T> getBeansOfType(Class<T> typeClass) {
        return applicationContext.getBeansOfType(typeClass);
    }

    /**
     * 根据BeanName获取Bean的类型
     * @param name
     * @return
     */
    public static Class<? extends Object> getType(String name) {
        return applicationContext.getType(name);
    }


    /**
     * 获取应用上下文
     * @return
     */
    public static Environment getEnvironment() {
        return applicationContext.getEnvironment();
    }

    /**
     * 获取程序配置属性
     * @return
     */
    public static String getProperty(String key) {
        return applicationContext.getEnvironment().getProperty(key);
    }

    /**
     * 获取程序配置属性
     * @return
     */
    public static <T> T getProperty(String key, Class<T> typeClass) {
        return applicationContext.getEnvironment().getProperty(key, typeClass);
    }

    /**
     * 获取当前环境
     * @return
     */
    public static String getActiveProfile() {
        return applicationContext.getEnvironment().getActiveProfiles()[0];
    }

    /**
     * 是否在开发环境
     * @return
     */
    public static boolean inDev() {
        return DEVELOPMENT_ENVIRONMENT.equalsIgnoreCase(getActiveProfile());
    }

    /**
     * 是否在测试环境
     * @return
     */
    public static boolean inTest() {
        return TEST_ENVIRONMENT.equalsIgnoreCase(getActiveProfile());
    }

    /**
     * 是否在线上环境
     * @return
     */
    public static boolean inProd() {
        return PRODUCT_ENVIRONMENT.equalsIgnoreCase(getActiveProfile());
    }

    /**
     * 是否不在线上环境
     * @return
     */
    public static boolean notInProd() {
        return !inProd();
    }
}
